// gjw/pages/order/index.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    menu:['全部','待付款','待发货','待收货','已完成'],
    index:0,
    activeIndex:0,
  },


  tabClick: function (e) {
    
    this.setData({
      activeIndex: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		var siteUrl = wx.getStorageSync('siteUrl')
    that.setData({
      siteUrl: siteUrl
    })
    that.getAll()
    that.one()
    that.two()
    that.three()
    that.four()
  },

  // 全部
  getAll:function(e){
    var that=this
    var page=that.data.page
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Personordersall',
      data: {
        openid: openid
      },
      success:function(res){
        
        if(res.data.code==500){
            that.setData({
              allList: []              
            })
        }else if(res.data.code==200){
             for (let i in res.data.res) {
                if (res.data.res[i].img != null) {
                  res.data.res[i].img = res.data.res[i].img.split(',')
                  that.setData({
                    allList: res.data.res
                  })
                } else {
                  that.setData({
                    allList: res.data.res
                  })
                }

              }
              
            }
            
        }
      
    })
  },



  // 待付款
  one: function (e) {
    var that = this
    var page = that.data.page
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Personorders',
      data: {
        openid: openid, page: page, state:1
      },
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            onelist: []
          })
        } else if (res.data.code == 200) {
          for (let i in res.data.res) {
            if (res.data.res[i].img != null) {
              res.data.res[i].img = res.data.res[i].img.split(',')
              that.setData({
                onelist: res.data.res
              })
            } else {
              that.setData({
                onelist: res.data.res
              })
            }

          }
        }
      }
    })
  },


  // 待发货
  two: function (e) {
    var that = this
    var page = that.data.page
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Personorders',
      data: {
        openid: openid, page: page, state: 2
      },
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            twolist: []
          })
        } else if (res.data.code == 200) {
          for (let i in res.data.res) {
            if (res.data.res[i].img != null) {
              res.data.res[i].img = res.data.res[i].img.split(',')
              that.setData({
                twolist: res.data.res
              })
            } else {
              that.setData({
                twolist: res.data.res
              })
            }

          }
        }
      }
    })
  },


  // 待收货
  three: function (e) {
    var that = this
    var page = that.data.page
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Personorders',
      data: {
        openid: openid, page: page, state: 3
      },
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            threelist: []
          })
        } else if (res.data.code == 200) {
          for (let i in res.data.res) {
            if (res.data.res[i].img != null) {
              res.data.res[i].img = res.data.res[i].img.split(',')
              that.setData({
                threelist: res.data.res
              })
            } else {
              that.setData({
                threelist: res.data.res
              })
            }

          }
        }
      }
    })
  },

  // 已完成
  four: function (e) {
    var that = this
    var page = that.data.page
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Personorders',
      data: {
        openid: openid, page: page, state: 4
      },
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            fourlist: []
          })
        } else if (res.data.code == 200) {
          for (let i in res.data.res) {
            if (res.data.res[i].img != null) {
              res.data.res[i].img = res.data.res[i].img.split(',')
              that.setData({
                fourlist: res.data.res
              })
            } else {
              that.setData({
                fourlist: res.data.res
              })
            }

          }
        }
      }
    })
  },
//为什么要写四个支付接口？唐问
  // 抖宝的商品支付
  dbpay:function(e){
    
    var openid = wx.getStorageSync('openid')
    var res_id = e.currentTarget.dataset.id
    var ofees=e.currentTarget.dataset.ofees
    app.util.request({
      url: 'entry/wxapp/Dborderpay',
      data: {
        openid: openid, res_id: res_id, ofees: ofees
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.util.request({
              url: 'entry/wxapp/Dborderpaysuccess',
              data: {
                res_id: res_id
              },
              success: function (res) {
                that.getAll()
                that.one() 
                that.two()
                that.three()
                that.four()                
                that.onde
                wx.showModal({
                  title: '提示',
                  content: '您的订单已经付款成功,商家会尽快为您发货哦',
                  showCancel: false,
                  cancelColor: '',
                  confirmText: '我知道了',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
               
              }
            })
            
          },
          'fail': function (res) {
            
            wx.showToast({
              title: '支付失败',
              icon: "loading",
            })
          },
         
        })

      }

    })
  },


  // 微拍的商品支付
  wppay: function (e) {
    
    var that=this
    var openid = wx.getStorageSync('openid')
    var res_id = e.currentTarget.dataset.id
    var g_id = e.currentTarget.dataset.goods
    var ofees = e.currentTarget.dataset.ofees
    app.util.request({
      url: 'entry/wxapp/Shotorderpay',
      data: {
        openid: openid, res_id: res_id, ofees: ofees
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.util.request({
              url: 'entry/wxapp/Shotorderpaysuccess',
              data: {
                res_id: res_id, p_id:g_id
              },
              success: function(res){
                that.getAll()
                that.one()
                that.two()
                that.three()
                that.four()
                wx.showModal({
                  title: '提示',
                  content: '您的订单已经付款成功,商家会尽快为您发货哦',
                  showCancel: false,
                  cancelColor: '',
                  confirmText: '我知道了',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })

              }
            })

          },
          'fail': function (res) {
            
            wx.showToast({
              title: '支付失败',
              icon: "loading",
            })
          },
          
        })

      }

    })
  },


  // 集市的商品支付
  jspay: function (e) {
    
    var openid = wx.getStorageSync('openid')
    var res_id = e.currentTarget.dataset.id
    var g_id = e.currentTarget.dataset.goods
    var ofees = e.currentTarget.dataset.ofees
    app.util.request({
      url: 'entry/wxapp/Marketorderpay',
      data: {
        openid: openid, res_id: res_id, ofees: ofees
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.util.request({
              url: 'entry/wxapp/Marketorderpaysuccess',
              data: {
                res_id: res_id, p_id: g_id
              },
              success: function (res) {
                that.getAll()
                that.one()
                that.two()
                that.three()
                that.four()
                wx.showModal({
                  title: '提示',
                  content: '您的订单已经付款成功,商家会尽快为您发货哦',
                  showCancel: false,
                  cancelColor: '',
                  confirmText: '我知道了',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })

              }
            })

          },
          'fail': function (res) {
            
            wx.showToast({
              title: '支付失败',
              icon: "loading",
            })
          },
          
        })

      }

    })
  },

  // 商店的商品支付
  sdpay: function (e) {
    
    var openid = wx.getStorageSync('openid')
    var res_id = e.currentTarget.dataset.id
    var g_id = e.currentTarget.dataset.goods
    var ofees = e.currentTarget.dataset.ofees
    app.util.request({
      url: 'entry/wxapp/Storeorderpay',
      data: {
        openid: openid, res_id: res_id, ofees: ofees
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.util.request({
              url: 'entry/wxapp/Storeorderpaysuccess',
              data: {
                res_id: res_id, p_id: g_id
              },
              success: function (res) {
                that.getAll()
                that.one()
                that.two()
                that.three()
                that.four()
                wx.showModal({
                  title: '提示',
                  content: '您的订单已经付款成功,商家会尽快为您发货哦',
                  showCancel: false,
                  cancelColor: '',
                  confirmText: '我知道了',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })

              }
            })

          },
          'fail': function (res) {
            
            wx.showToast({
              title: '支付失败',
              icon: "loading",
            })
          },
          
        })

      }

    })
  },

  waite:function(e){
    wx.showModal({
      title: '提示',
      content: '您的订单将在72小时内为您发货，请耐心等待...',
      showCancel:false,
      confirmText:'我知道了',
      success: function (res) {
        if (res.confirm) {
          
        } else if (res.cancel) {
          
        }
      }
    })
  },

  // 确认收货
  get_gift:function(e){
    var that=this
        
    var res_id = e.currentTarget.dataset.id   
    var g_id = e.currentTarget.dataset.goods     
    var cusname = e.currentTarget.dataset.goods  
    var type = e.currentTarget.dataset.type  
    wx.showModal({
      title: '提示',
      content: '是否确认收到宝贝，并且验收完好？',
      success: function (res) {
        if (res.confirm) {
          
          app.util.request({
            url: 'entry/wxapp/Usersh',
            data: {
              res_id: res_id,
            },
            success: function (res) {
              
              that.getAll()
              that.three()
              that.four()
              wx.showModal({
                title: '提示',
                content: '您已成功收货，去给店铺评个分吧',
                success:function(res){
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../order/comment?id=' + res_id + '&p_id=' + g_id + '&cusname=' + cusname + '&type=' + type
                    })
                  }
                }
              })
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })
    
  },

  // 删除订单
  delete: function (e) {
    var that = this
    
    var res_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确定删除该订单？',
      success: function (res) {
        if (res.confirm) {
          
          app.util.request({
            url: 'entry/wxapp/Orderdelete',
            data: {
              id: res_id,
            },
            success: function (res) {
              
              that.getAll()
              that.four()
              wx.showToast({
                title: '已成功删除',
              })
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })

  },


  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     var that=this
     var activeIndex=that.data.activeIndex
     if(activeIndex==0){
       that.getAll()
     }else if(activeIndex==1){
       that.one()
     }else if(activeIndex==2){
       that.two()
     }else if(activeIndex==3){
       that.three()
     } else if (activeIndex==4){
       that.four()
     }
     wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // wx.showLoading({
    //   title: '玩命加载中',
    // })
    var that = this
    that.data.page = that.data.page + 1
    var i = that.data.activeIndex
    var openid = wx.getStorageSync('openid')
    if (i == 0) {
      app.util.request({
        'url': 'entry/wxapp/Personordersall',
        'cachetime': '0',
        data: { openid: openid,page: that.data.page },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          }else if (res.data.code == 200) {

            for (var i = 0; i < res.data.res.length; i++) {
              res.data.res[i].img = res.data.res[i].img.split(",")
            }
            for (var j= 0; j < res.data.res.length; j++) {
              that.data.allList.push(res.data.res[j])
            }
            that.setData({
              allList: that.data.allList
            })
          }
        }
      })
    }else if(i==1){
      app.util.request({
        'url': 'entry/wxapp/Personorders',
        'cachetime': '0',
        data: { openid: openid, page: that.data.page,state:1 },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

            for (var i = 0; i < res.data.res.length; i++) {
              res.data.res[i].img = res.data.res[i].img.split(",")
            }
            for (var j = 0; j < res.data.res.length; j++) {
              that.data.onelist.push(res.data.res[j])
            }
            that.setData({
              onelist: that.data.onelist
            })
          }
        }
      })
    } else if (i == 2) {
      app.util.request({
        'url': 'entry/wxapp/Personorders',
        'cachetime': '0',
        data: { openid: openid, page: that.data.page, state: 2 },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

            for (var i = 0; i < res.data.res.length; i++) {
              res.data.res[i].img = res.data.res[i].img.split(",")
            }
            for (var j = 0; j < res.data.res.length; j++) {
              that.data.twolist.push(res.data.res[j])
            }
            that.setData({
              twolist: that.data.twolist
            })
          }
        }
      })
    } else if (i ==3) {
      app.util.request({
        'url': 'entry/wxapp/Personorders',
        'cachetime': '0',
        data: { openid: openid, page: that.data.page, state: 3 },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

            for (var i = 0; i < res.data.res.length; i++) {
              res.data.res[i].img = res.data.res[i].img.split(",")
            }
            for (var j = 0; j < res.data.res.length; j++) {
              that.data.threelist.push(res.data.res[j])
            }
            that.setData({
              threelist: that.data.threelist
            })
          }
        }
      })
    } else if (i == 4) {
      app.util.request({
        'url': 'entry/wxapp/Personorders',
        'cachetime': '0',
        data: { openid: openid, page: that.data.page, state:4 },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

            for (var i = 0; i < res.data.res.length; i++) {
              res.data.res[i].img = res.data.res[i].img.split(",")
            }
            for (var j = 0; j < res.data.res.length; j++) {
              that.data.fourlist.push(res.data.res[j])
            }
            that.setData({
              fourlist: that.data.fourlist
            })
          }
        }
      })
    }
  },
  linkClick:function(e){
    wx.reLaunch({
      url: '../member/index',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})